package vn.ivs.taxiapp.fragments;

import vn.ivs.taxiapp.R;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;

import com.google.android.gms.maps.SupportMapFragment;

public class S001Fragment extends Fragment implements OnClickListener {

	// Local controls
	private Button btnFindTheWay, btnFindTheFee;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View v = inflater.inflate(R.layout.fragment_s001, container, false);

		btnFindTheWay = (Button) v.findViewById(R.id.btnFindTheWay);
		btnFindTheFee = (Button) v.findViewById(R.id.btnFindTheFee);

		btnFindTheWay.setOnClickListener(this);
		btnFindTheFee.setOnClickListener(this);

		return v;
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btnFindTheWay:
			goToS002();
			break;

		case R.id.btnFindTheFee:
			goToS004();
			break;

		default:

			break;
		}

	}

	// Go to S002: Where you want to go
	private void goToS002() {
		if(S003Fragment.googleMap != null) {
			reloadMap();
			S003Fragment.setUpMapIfNeeded();
			S003Fragment.setUpMap();
			
		}
		Fragment fragmentS003 = new S003Fragment();
		FragmentTransaction ft = getFragmentManager().beginTransaction();
		ft.add(android.R.id.tabcontent, fragmentS003);

		ft.addToBackStack(null);
		ft.commit();

	}

	// Go to S004: How much you can pay
	private void goToS004() {
		Fragment fragmentS002 = new S002Fragment();
		String tag = fragmentS002.getTag();
		FragmentTransaction ft = getFragmentManager().beginTransaction();
		ft.add(android.R.id.tabcontent, fragmentS002);

		ft.addToBackStack(tag);
		ft.commit();
	}
	
	public void reloadMap() {
		SupportMapFragment f = (SupportMapFragment) getFragmentManager()
				.findFragmentById(R.id.map);
		if (f != null) {
			try {
				getFragmentManager().beginTransaction().remove(f).commit();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		S003Fragment.googleMap = null;
	}

}
